/*
 * xAPBSClevel.java
 *
 * Java class used to store xAP BSC level information
 *
 * Copyright (c) 2009 Daniel Berenguer <dberenguer@usapiens.com>
 *
 * This file is part of the jxAP project.
 *
 * OPNODE is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * OPNODE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with canTracer; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * 12/08/09 by Daniel Berenguer : first version
 */

package xAP;


/********************************************************************************************
* xAPBSClevel
*
* xAP BSC level class
*
********************************************************************************************/
public class xAPBSClevel
{
    // Member variables
    private long value;
    private long total;
    private String strLevel;

   /********************************************************************************************
    * xAPBSClevel
    *
    * xAP BSC level constructor
    *
    * 'strLevel'    xAP BSC level string
    *
    ********************************************************************************************/
    public xAPBSClevel(String Level)
    {
        strLevel = Level;

        try
        {
            xAPBSClevelSplit(strLevel);
        }
        catch(xAPexception e)
        {
            e.display();
        }
    }

   /********************************************************************************************
    * updateFromValue
    *
    * Update BSC level from value
    *
    * 'newVal'      new value
    *
    ********************************************************************************************/
    public void updateFromValue(int newVal)
    {
        value = newVal;
        strLevel = value + "/" + total;
    }
    public void updateFromValue(byte newVal)
    {
        value = newVal;
        strLevel = value + "/" + total;
    }
    public void updateFromValue(double newVal)
    {
        value = (int)newVal;
        strLevel = value + "/" + total;
    }    

   /********************************************************************************************
    * getTotal
    *
    * Get total quantity From BSC level
    *
    * Return:
    *   total quantity from level/total
    ********************************************************************************************/
    public long getTotal()
    {
        return total;
    }

   /********************************************************************************************
    * setTotal
    *
    * Set total quantity for BSC level
    *
    * 'Total'       Total number for level/total
    *
    ********************************************************************************************/
    public void setTotal(int Total)
    {
        total = Total;
        strLevel = value + "/" + total;
    }

   /********************************************************************************************
    * levelToInteger
    *
    * Convert BSC level into integer value
    *
    ********************************************************************************************/
    public double toDouble()
    {
        return (double)value;
    }

   /********************************************************************************************
    * levelToInteger
    *
    * Convert BSC level into integer value
    *
    ********************************************************************************************/
    public int toInteger()
    {
        return (int)value;
    }

   /********************************************************************************************
    * levelToByte
    *
    * Convert BSC level into byte value
    *
    ********************************************************************************************/
    public byte toByte()
    {
        return (byte)value;
    }

   /********************************************************************************************
    * levelToString
    *
    * Convert BSC level into string
    *
    ********************************************************************************************/
    public String toString()
    {
        return strLevel;
    }

   /********************************************************************************************
    * xAPBSClevelSplit
    *
    * Split BSC level string into value/total
    *
    * 'strLevel'    xAP BSC level string
    *
    ********************************************************************************************/
    private void xAPBSClevelSplit(String strLevel) throws xAPexception
    {
        int intPos;

        // split string into value/total
        if ((intPos = strLevel.indexOf('/')) != -1)
        {
            value = Long.parseLong(strLevel.substring(0, intPos));
            intPos++;
            total = Long.parseLong(strLevel.substring(intPos));
        }
        else if ((intPos = strLevel.indexOf('%')) != -1)
        {
            value = Long.parseLong(strLevel.substring(0, intPos));
            total = 100;
        }
        else
            throw new xAPexception("\"" + strLevel + "\" is not a valid BSC level");
    }
}